﻿using Esint.JudicialAuction.BLL;
using Esint.JudicialAuction.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebJudicialAuction.admin.bases;
using Esint.Common;

namespace WebJudicialAuction.admin.handler
{
    /// <summary>
    /// userManagement 的摘要说明
    /// </summary>
    public class userManagement : BaseHandler, IHttpHandler
    {
        public userManagement()
        {
            IgnoreUserLogin = true;
        }

        Sys_UserService userBll = new Sys_UserService();

        protected override AjaxResult BusinessHandleFunc(string req_type, Dictionary<string, string> dicData)
        {
            AjaxResult ajaxResult = new AjaxResult();

            switch (req_type)
            {
                default:
                    break;
                case "GetPagedList":
                    {
                        var list = userBll.GetMainList(dicData["searchUserName"], dicData["searchCellphone"], dicData["searchUserType"], dicData["searchStatus"], int.Parse(dicData["pgSize"]), int.Parse(dicData["pgIndex"]));
                        if (list != null)
                        {
                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.data = list;
                            ajaxResult.msg = AjaxMessage.SearchSuccess;
                        }
                        else
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.SearchFailed;
                        }

                        break;
                    }

                case "GetModelDetail":
                    {
                        var model = userBll.GetSys_UserInfo(dicData["id"]);

                        if (model != null)
                        {
                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.data = model;
                            ajaxResult.msg = AjaxMessage.LoadInfoSuccess;
                        }
                        else
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.LoadInfoFailed;
                        }

                        break;
                    }

                case "SaveModelInfo":
                    {
                        var model = JsonConvert.DeserializeObject<Sys_UserInfo>(dicData["modelInfo"]);
                        var timeNow = DateTime.Now;
                        if (model.UserId.IsNullOrEmpty())
                        {
                            model.CreateTime = timeNow;
                            model.CreateUserId = CurrentUserId;
                            model.IsDelete = "0";
                        }
                        model.OpTime = timeNow;
                        model.OpUserId = CurrentUserId;

                        model.ValidData();

                        userBll.Save(model);

                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.SaveSuccess;

                        break;
                    }

                case "ResetPassword":
                    {
                        var model = userBll.GetSys_UserInfo(dicData["id"]);
                        if (model.IsNotNull())
                        {
                            model.UserPwd = CommonExtend.GetAppSetting("DefaultPwd");
                            userBll.Save(model);

                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.msg = AjaxMessage.OperationSuccess;
                        }
                        else
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.NotFoundData;
                        }

                        break;
                    }

                case "DeleteUser":
                    {
                        JArray jArray = JArray.Parse(dicData["ids"]);
                        List<string> idList = (from t in jArray select t.Value<string>()).ToList();

                        userBll.DeleteUsers(idList);

                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.OperationSuccess;

                        break;
                    }
                case "SetDisabled":
                    {
                        JArray jArray = JArray.Parse(dicData["ids"]);
                        List<string> idList = (from t in jArray select t.Value<string>()).ToList();

                        userBll.DisabledUsers(idList, dicData["opFlag"]);

                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.OperationSuccess;

                        break;
                    }
            }

            return ajaxResult;
        }
    }
}